<?php 

class LsPhpMessageSource
extends CPhpMessageSource
{
	
	public $_plugin_files=array();
	
	/**
	 * @param string $language the language that the source messages are written in.
	 */
	public function setLanguage($language){
		$language = LsLocale::getCanonicalID($language);
		parent::setLanguage($language);
	}
	
	/**
	 * Initializes the application component.
	 * This method overrides the parent implementation by preprocessing
	 * the user request data.
	 */
	public function init()
	{
		if($this->basePath===null){
			$this->basePath=Yii::getPathOfAlias('application');
		}
		parent::init();
	}
	
	
	/**
	 * Determines the message file name based on the given category and language.
	 * If the category name contains a dot, it will be split into the module class name and the category name.
	 * In this case, the message file will be assumed to be located within the 'messages' subdirectory of
	 * the directory containing the module class file.
	 * Otherwise, the message file is assumed to be under the {@link basePath}.
	 * @param string $category category name
	 * @param string $language language ID
	 * @return string the message file path
	 * @since 1.0.10
	 */
	protected function getMessageFile($category,$language)
	{
		if(preg_match('#^p_(\w+)(.(.+))?$#', $category, $t)){
			$plugin = $t[1];
			$cat = isset($t[3]) ? strtr($t[3], array('.' => DIRECTORY_SEPARATOR)) : null;
			if(!isset($this->_plugin_files[$plugin][$language])){
				$this->_plugin_files[$plugin][$language] = 
					Plugin::GetPath($plugin).'include'
					.DIRECTORY_SEPARATOR.'messages'
					.(isset($cat) ? DIRECTORY_SEPARATOR.$cat : '')
					.DIRECTORY_SEPARATOR.$language.'.php'
				;
			}
			return $this->_plugin_files[$plugin][$language];
		}
		return parent::getMessageFile($category,$language);
	}
	
}

?>